package com.fjc.controller;

import com.alibaba.fastjson.JSONObject;
import com.fjc.domain.ResultObject;
import com.fjc.domain.SysUser;
import com.fjc.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by fjc on 2018/5/22.
 */
@RestController
@RequestMapping("/user")
public class SysUserController {
    private final static Logger logger = LoggerFactory.getLogger(SysUserController.class);
    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("/login")
    public ResultObject login(String username, String password,boolean rememberMe,HttpServletRequest request){
        logger.info("接收到username:" + username + ",password:" + password + ",rememberMe" + rememberMe);
        return sysUserService.login(username,password,rememberMe,request);
    }

    @RequestMapping("/logout")
    public ResultObject logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        logger.info("推出登录成功");
        return new ResultObject(0,"");
    }

    @RequestMapping("/getUserInfo")
    @RequiresRoles({"admin"})
    public ResultObject getUserInfo(){
        SysUser user = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("userInfo");
        logger.info("获取session用户信息成功" + user);
        return new ResultObject(0,"获取成功",user);
    }

    @RequestMapping("/re")
    @RequiresPermissions("user")
//    @RequiresPermissions("user:createUser")
    public String red(){
        return "";
    }

    @GetMapping("/getNavPermissions")
    public ResultObject getNavPermissions(){
        return sysUserService.getNavPermissions();
    }

    @GetMapping("getLoginLog")
    public ResultObject getLoginLog(Integer page,Integer limit){
        logger.info("用户登录日志page:" + page + " limit:" + limit);
        return sysUserService.getLoginLog(page,limit);
    }

    @GetMapping("getUsersInfo")
    public ResultObject getUsersInfo(Integer page,Integer limit){
        logger.info("用户信息page:" + page + " limit:" + limit);
        return sysUserService.getUsersInfo(page,limit);
    }

    @GetMapping("delUsersInfo")
    public ResultObject delUsersInfo(Integer id){
        logger.info("删除用户" + id);
        return sysUserService.delUsersInfo(id);
    }

    @PostMapping("saveUser")
    public ResultObject saveUser(SysUser sysUser){
        logger.info("保存用户" + sysUser);
        return sysUserService.saveUser(sysUser);
    }

    @GetMapping("getUserPermissions")
    public ResultObject getUserPermissions(Integer id){
        logger.info("获取用户权限树" + id);
        return sysUserService.getUserPermissions(id);
    }

    @PostMapping("updatePermissionsByUserId")
    public ResultObject updatePermissionsByUserId(@RequestBody JSONObject object){
        logger.info("更新用户权限树" + object);
        return sysUserService.updatePermissionsByUserId(object);

    }

}
